/*! *********************************************************************************
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* \file
*
* LED implementation file for ARM CORTEX-M4 processor
*
* SPDX-License-Identifier: BSD-3-Clause
********************************************************************************** */

#include "LED.h"
#include "TimersManager.h"
#include "fsl_os_abstraction.h"




/******************************************************************************
*******************************************************************************
* Private macros
*******************************************************************************
******************************************************************************/







#define gLedId_LastActiveId_c           (0x01 << (gLEDsOnTargetBoardCnt_c-1))
/******************************************************************************
*******************************************************************************
* Private type definitions
*******************************************************************************
******************************************************************************/

/*
* Name: LED_DimMode_t
* Description: RGB LED dimming modes
*/
typedef enum LED_DimMode_tag {
  gLedDimIn_c,
  gLedDimOut_c,
  gLedDimMax_c
} LED_DimMode_t;


/******************************************************************************
*******************************************************************************
* Public memory definitions
*******************************************************************************
******************************************************************************/

/******************************************************************************
*******************************************************************************
* Private memory definitions
*******************************************************************************
******************************************************************************/

/******************************************************************************
*******************************************************************************
* Private function prototypes
*******************************************************************************
******************************************************************************/

#if gTMR_Enabled_d
static void LED_FlashTimeout(uint8_t timerId);
#endif



/******************************************************************************
*******************************************************************************
* Private type definitions
*******************************************************************************
******************************************************************************/

/******************************************************************************
*******************************************************************************
* Private Memory Declarations
*******************************************************************************
******************************************************************************/

/* LED timer ID */
static tmrTimerID_t mLEDTimerID = gTmrInvalidTimerID_c;


#if gTMR_Enabled_d && gLedRgbEnabled_d && gRgbLedDimmingEnabled_d
/*
 * Name: mRbgDimInfo
 * Description: RGB dimming info structure. Contains dimming related information.
 */
static LED_RgbDimInfo_t mRbgDimInfo;
#endif

/******************************************************************************
******************************************************************************
* Public functions
******************************************************************************
*****************************************************************************/

/******************************************************************************
* Name: LED_Init
* Description: Initialize the LED module
* Parameters: -
* Return: -
******************************************************************************/
void LED_Init
(
    void
)
{
    STM_EVAL_LEDInit(LED3);
    STM_EVAL_LEDInit(LED4);
    
    /* allocate a timer for use in flashing LEDs */
    mLEDTimerID = TMR_AllocateTimer();

    

}

/******************************************************************************
* Name: Un-initialize the LED module
* Description: turns off all the LEDs and disables clock gating for LED port
* Parameters: -
* Return: -
******************************************************************************/
void LED_UnInit
(
void
)
{


#if gTMR_Enabled_d
    /* free the timer used for flashing mode */
    TMR_FreeTimer(mLEDTimerID);
#endif
    
}




/******************************************************************************
* Name: LED_StopFlash
* Description: Stop an LED from flashing.
* Parameters: [IN] LED_t LEDNr - LED Number (may be an OR of the list)
* Return: -
******************************************************************************/
void LED_StopFlash
(
    LED_t LEDNr
)
{

        TMR_StopTimer(mLEDTimerID);

}


/******************************************************************************
* Name: LED_StartFlashWithPeriod
* Description: Starts flashing one or more LEDs
* Parameters: [IN] LED_t LEDNr          - LED Number (may be an OR of the list)
              [IN] uint16_t periodMs    - LED flashing period in milliseconds 
* Return: -
******************************************************************************/
void LED_StartFlashWithPeriod(LED_t LEDNr, uint16_t periodMs)
{
    /* indicate which LEDs are flashing */
    //mLedFlashingLEDs |= LEDNr;

#if gTMR_Enabled_d
    /* start the timer */
    if(!TMR_IsTimerActive(mLEDTimerID))
    {
        TMR_StartIntervalTimer(mLEDTimerID, periodMs, (pfTmrCallBack_t)LED_FlashTimeout, (void*)((uint32_t)mLEDTimerID));
    }
#else
    #warning "The TIMER component is not enabled and therefore the LED flashing function is disabled"
#endif
}

/******************************************************************************
* Name: LED_FlashTimeout
* Description: timer callback function that is called each time the timer
*              expires
* Param(s): [IN] timerId - the timer ID
* Return: -
******************************************************************************/
static void LED_FlashTimeout
(
    uint8_t timerId
)
{
    timerId = timerId;  /* prevent compiler warning */
    
    STM_EVAL_LEDToggle(LED3);

}





